package savet.gui.tabelas;

import java.awt.Component;
import java.awt.Point;
import java.awt.event.ActionEvent;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.AbstractAction;
import javax.swing.AbstractCellEditor;
import javax.swing.Action;
import javax.swing.DefaultCellEditor;
import javax.swing.JButton;
import javax.swing.JTable;
import javax.swing.table.TableCellEditor;
import org.openswing.swing.mdi.client.MDIFrame;
import savet.entidades.atributos.Atributo;
import savet.gui.dialogos.DialogoLegendas;

/**
 *
 * @author Adisson
 */
public class AtributoCellEditor extends AbstractCellEditor
        implements TableCellEditor {

    private static int contadorTemporario = 0;
    DefaultCellEditor d;
    private JButton editar;
    private Atributo atributo;

    private int countClick = 0;
    private Point lastCellSelected = null;
    private Class atributoRelacionado;

    public AtributoCellEditor(Class atributoRelacionado) {
        this.atributoRelacionado = atributoRelacionado;
        editar = new JButton();
        editar.setBorderPainted(false);
        editar.setContentAreaFilled(false);
        editar.addActionListener(getOkAction());
    }

    public Component getTableCellEditorComponent(JTable table, Object value, 
                                                 boolean isSelected,
                                                 int row, int column) {
        verificaUltimaSelecao(row, column);
        atributo = (Atributo) value;
        return editar;
    }

    private void verificaUltimaSelecao(int row, int column) {
        final Point currentCell = new Point(row, column);
        if (lastCellSelected == null)
            lastCellSelected = currentCell;
        
        if(!lastCellSelected.equals(currentCell)) {
            lastCellSelected = currentCell;
            countClick = 0;
        }
    }

    public final Action getOkAction() {
        return new AbstractAction("Add") {

            public void actionPerformed(ActionEvent e) {
                countClick ++;
              Thread t= new Thread(new Runnable() {

                    public void run() {
                        try {
                            Thread.sleep(450);
                            countClick = 0;
                        } catch (InterruptedException ex) {
                            Logger.getLogger(AtributoCellEditor.class.getName()).log(Level.SEVERE, null, ex);
                        }
                    }
                });
                t.start();
                if (countClick == 2) {
                    DialogoLegendas dialogoEmentas = new DialogoLegendas(MDIFrame.getInstance(), true, atributo,atributoRelacionado);
                    dialogoEmentas.setLocationRelativeTo(null);
                    dialogoEmentas.setVisible(true);

                    countClick = 0;
                }
                fireEditingStopped();
            }
        };
    }

    public Object getCellEditorValue() {
        return atributo;
    }

    public void setAtributo(Atributo atributo) {
        this.atributo = atributo;
    }

    public Atributo getAtributo() {
        return atributo;
    }

    public Class getAtributoRelacionado() {
        return atributoRelacionado;
    }

    public void setAtributoRelacionado(Class atributoRelacionado) {
        this.atributoRelacionado = atributoRelacionado;
    }

}
